﻿//   BinarySerializer.cs
//   Copyright (c) 2012 Eswar Prakash
// 
//    This library is free software; you can redistribute it and/or modify  
//    it  under the terms of the GNU Lesser General Public License version  
//    2.1 as published by the Free Software Foundation.                     
//                                                                          
//    This library is distributed in the hope that it will be useful, but   
//    WITHOUT ANY WARRANTY; without even the implied warranty of            
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     
//    Lesser General Public License for more details.                       
//                                                                          
//    You should have received a copy of the GNU Lesser General Public      
//    License along with this library; if not, write to the Free Software   
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
//    USA
namespace CKSAS.Core.Utilities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;


    /// <summary>
    /// Provides methods to serialize an object to bytes for storage
    /// </summary>
    public static class BinarySerializer
    {
        /// <summary>
        /// Serializes the specified object to a byte array. The type of the object should be marked
        /// with <see cref="SerializableAttribute"/>.
        /// </summary>
        /// <param name="obj">The object of type <typeparamref name="T"/> to serialize.</param>
        /// <returns>An array <see cref="byte"/> representing a serialized version of the object</returns>
        public static byte[] Serialize<T>(T obj) where T : class
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type is not marked as serializable", "obj");
            }

            byte[] serializedBytes = null;

            // use memory stream to serialize and convert
            using (var memoryStream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, obj);

                serializedBytes = memoryStream.ToArray();
            }

            return serializedBytes;
        }

        /// <summary>
        /// Deserializes the specified bytes to an object. The type of the object should be marked using
        /// <see cref="SerializableAttribute"/>.
        /// </summary>
        /// <param name="serializedBytes">An array of <see cref="byte"/> that contains the serialized object.</param>
        /// <returns>An instance of type <typeparamref name="T"/></returns>
        public static T Deserialize<T>(byte[] serializedBytes) where T : class
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type is not marked as serializable", "T");
            }

            using (var memoryStream = new MemoryStream(serializedBytes, false))
            {
                var formatter = new BinaryFormatter();
                var deserialized = formatter.Deserialize(memoryStream);

                return (T)deserialized;
            }
        }
    }
}